import { writable, get } from "svelte/store";

// user: {userId: string, username: string, role: string  }
const savedUser = localStorage.getItem("user");
const savedToken = localStorage.getItem("token");
export const user = writable(savedUser ? JSON.parse(savedUser) : null);
export const token = writable(savedToken ? savedToken : null);

user.subscribe((value) => {
  console.log("user changed ", value);
  if (value) {
    localStorage.setItem("user", JSON.stringify(value));
  } else {
    localStorage.removeItem("user");
  }
});
token.subscribe((value) => {
  console.log("token changed ", value);
  if (value) {
    localStorage.setItem("token", value);
  } else {
    localStorage.removeItem("token");
  }
});
